package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.common.constant.BusinessStatusConstants;
import com.qk.domain.business.BusinessDO;
import com.qk.domain.business.BusinessPoolDO;
import com.qk.dto.business.BusinessDTO;
import com.qk.dto.business.BusinessListDTO;
import com.qk.dto.business.BusinessListPoolDTO;
import com.qk.dto.business.UpdateBusinessInfoDTO;
import com.qk.entity.Business;
import com.qk.management.aop.anno.LogAnno;
import com.qk.management.service.BusinessService;
import com.qk.vo.business.BusinessVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;


/**
 * @author 李智勇
 * @data 2025/10/15 11:25
 */

@RestController
@RequestMapping("/businesses")
public class BusinessController {
    @Autowired
    private BusinessService businessService;
    @GetMapping("/pool")
    public Result listPoolBusiness(BusinessListPoolDTO dto) {
        PageResult<BusinessPoolDO>  result = businessService.listPoolBusiness(dto);
        return Result.success(result);
    }


    @LogAnno
    @PutMapping("/back/{id}")
    public Result updateBusinessStatusToBack(@PathVariable Integer id) {
        businessService.updateById(Business.builder()
                .id(id)
                .status(BusinessStatusConstants.RECOVERY)
                .updateTime(LocalDateTime.now())
                .build());
        return Result.success();
    }
    @LogAnno
    @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id) {
        businessService.toCustomer(id);
        return Result.success();
    }
    @LogAnno
    @PutMapping
    public Result updateBusinessInfoById(@RequestBody UpdateBusinessInfoDTO dto) {
        businessService.updateBusinessInfoById(dto);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getBusinessInfoById(@PathVariable Integer id){
        BusinessVO vo= businessService.getBusinessInfoById(id);
        return Result.success(vo);
    }
    @LogAnno
    @PutMapping("/assign/{businessId}/{userId}")
    public Result updateBusinessById(@PathVariable Integer businessId, @PathVariable Integer userId) {
        businessService.updateById(Business.builder()
                .id(businessId)
                .userId(userId)
                .updateTime(LocalDateTime.now())
                .status( BusinessStatusConstants.WAIT_FOLLOW_UP)
                .build());
        return Result.success();
    }

    @GetMapping
    public Result list(BusinessListDTO dto) {
        PageResult<BusinessDO> result = businessService.businessList(dto);
        return Result.success(result);
    }
    @LogAnno
    @PostMapping
    public Result addBusiness(@RequestBody BusinessDTO dto) {
        businessService.businessadd(dto);
        return Result.success();
    }
}
